package flc.ast.activity;

import android.content.Intent;
import android.media.AudioManager;
import android.media.MediaPlayer;
import android.net.Uri;
import android.os.Bundle;
import android.os.Handler;
import android.view.View;

import androidx.activity.EdgeToEdge;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.graphics.Insets;
import androidx.core.view.ViewCompat;
import androidx.core.view.WindowInsetsCompat;

import com.blankj.utilcode.util.VolumeUtils;
import com.hpplay.sdk.source.api.LelinkPlayerInfo;
import com.hpplay.sdk.source.api.LelinkSourceSDK;
import com.stark.cast.screen.CastScreenManager;
import com.stark.picselect.entity.SelectMediaEntity;

import java.util.ArrayList;
import java.util.List;

import flc.ast.BaseAc;
import flc.ast.R;
import flc.ast.databinding.ActivityProVideoBinding;
import flc.ast.utils.AppSpUtils;
import flc.ast.utils.Content;
import stark.common.basic.event.EventStatProxy;
import stark.common.basic.utils.MediaUtil;
import stark.common.basic.utils.TimeUtil;
import stark.common.basic.utils.UriUtil;

public class ProVideoActivity extends BaseAc<ActivityProVideoBinding> {

    public static String videoPath;
    private long videoLength;
    private CastScreenManager mCastScreenManager;

    @Override
    protected int onCreate() {
        return R.layout.activity_pro_video;
    }

    @Override
    protected void initView() {
        EventStatProxy.getInstance().statEvent1(this, mDataBinding.event1Container);

        mDataBinding.ivProVideoBack.setOnClickListener(v -> getOnBackPressedDispatcher().onBackPressed());

        mDataBinding.ivSelVideo.setOnClickListener(this);
        mDataBinding.ivPlay.setOnClickListener(this);
        mDataBinding.tvRetrogress.setOnClickListener(this);
        mDataBinding.tvFastForward.setOnClickListener(this);
        mDataBinding.ivVolumeAdd.setOnClickListener(this);
        mDataBinding.ivVolumeReduce.setOnClickListener(this);

        mCastScreenManager = CastScreenManager.getInstance();
    }

    @Override
    protected void onClickCallback(View v) {
        int id = v.getId();
        if (id == R.id.ivSelVideo) {//选择视频
            Intent intent = new Intent(this, SelVideoActivity.class);
            intent.putExtra(Content.Again, "Again");
            startActivityForResult(intent, Content.PRO_SEL);
        }
        if (id == R.id.ivPlay) {//播放/暂停
            if (mDataBinding.videoView.isPlaying()) {
                mDataBinding.ivPlay.setImageResource(R.drawable.azt);
                mDataBinding.videoView.pause();
            } else {
                mDataBinding.ivPlay.setImageResource(R.drawable.abf);
                mDataBinding.videoView.start();
            }
        } else if (id == R.id.tvRetrogress) {//倒退10s
            // 获取当前播放位置（毫秒）
            int currentPos = mDataBinding.videoView.getCurrentPosition();
            // 倒退10秒（10000毫秒）并确保不小于0
            int newPos = Math.max(0, currentPos - 10000);
            mDataBinding.videoView.seekTo(newPos);
        } else if (id == R.id.tvFastForward) {//快进10s
            // 获取当前播放位置（毫秒）
            int currentPos = mDataBinding.videoView.getCurrentPosition();
            // 倒退10秒（10000毫秒）并确保大小于时长
            int newPos = (int) Math.max(videoLength, currentPos + 10000);
            mDataBinding.videoView.seekTo(newPos);
        } else if (id == R.id.ivVolumeAdd) {//音量+
            int volume = VolumeUtils.getVolume(AudioManager.STREAM_MUSIC);
            int max = VolumeUtils.getMaxVolume(AudioManager.STREAM_MUSIC);
            if (volume < max) {
                VolumeUtils.setVolume(AudioManager.STREAM_MUSIC, volume + 1, 0);
            }
        } else if (id == R.id.ivVolumeReduce) {//音量-
            int volume = VolumeUtils.getVolume(AudioManager.STREAM_MUSIC);
            int min = VolumeUtils.getMinVolume(AudioManager.STREAM_MUSIC);
            if (volume > min) {
                VolumeUtils.setVolume(AudioManager.STREAM_MUSIC, volume - 1, 0);
            }
        }
    }

    @Override
    protected void initData() {
        setPlayer();
        proVideo();
        saveRecord();
    }

    //视频投屏
    private void proVideo() {
        LelinkPlayerInfo playerInfo = new LelinkPlayerInfo();
        playerInfo.setType(LelinkSourceSDK.MEDIA_TYPE_VIDEO);
        Uri uri = Uri.parse(videoPath);
        if (UriUtil.isContentSchemeUri(uri)) {
            playerInfo.setLocalUri(uri);
        } else {
            playerInfo.setLocalPath(videoPath);
        }
        mCastScreenManager.startPlay(playerInfo);
    }

    private void setPlayer() {
        videoLength = MediaUtil.getDuration(videoPath);
        mDataBinding.videoView.setVideoPath(videoPath);
        mDataBinding.videoView.seekTo(1);
        mDataBinding.videoView.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
            @Override
            public void onCompletion(MediaPlayer mediaPlayer) {
                mDataBinding.ivPlay.setImageResource(R.drawable.azt);
            }
        });
        //直接播放视频
        mDataBinding.videoView.seekTo(1);
        mDataBinding.ivPlay.setImageResource(R.drawable.abf);
        mDataBinding.videoView.start();
    }

    private void saveRecord() {
        List<String> listSave = new ArrayList<>();
        listSave.add(videoPath);
        List<String> listSp = AppSpUtils.getVideoList();
        if (listSp != null && listSp.size() > 0) {
            listSp.addAll(listSave);
            AppSpUtils.saveVideoList(listSp);
        } else {
            AppSpUtils.saveVideoList(listSave);
        }
    }

    @Override
    protected void onStop() {
        super.onStop();
        mDataBinding.ivPlay.setImageResource(R.drawable.abf);
        mDataBinding.videoView.pause();
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (resultCode == RESULT_OK) {
            String path = data.getStringExtra(Content.selPath);
            if (requestCode == Content.PRO_SEL) {
                videoPath = path;
                proVideo();
                setPlayer();
            }

        }
    }

}